package org.concordia.wsdl.helper;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.concordia.wsdl.model.Problem;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


public class ProblemWrapper{
	
	//contains path of the composition problem file
	String path;
	
	//defines an array of Problem objects
	//Problem[] problems;
	public List<Problem> problems = new LinkedList<Problem>();
	
	//contains DOM representation of the composition problem
	//file  
	Document dom;
	
	public ProblemWrapper(){
		
	}
	
	public ProblemWrapper(String filePath){
		
		//sets the path of the composition problem file
		path=filePath;
	}
	
	//reads the problems and their characteristics from the
	//relevant file and stores it in Problems array
	public void doParse(String filePath) {
		
		//sets the path of the composition problem file
		path=filePath;
		
		//parse the XML file which contains the composition
		//problem and get the dom object
		parseXmlFile();
		
		//get every CompositionRoutine  element of the file 
		//and create a Problem object for each one
		parseDocument();
		
		printProblems();
	}
	
private void parseXmlFile(){
		
		//get the factory
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		
		try {
			
			//Using factory, get an instance of document 
			//builder
			DocumentBuilder db = dbf.newDocumentBuilder();
			
			//parse using builder to get DOM representation 
			//of the XML file
			dom = db.parse(path);
			
			
		}catch(ParserConfigurationException pce) {
			pce.printStackTrace();
		}catch(SAXException se) {
			se.printStackTrace();
		}catch(IOException ioe) {
			ioe.printStackTrace();
		}
	}
	
	private void parseDocument(){
		

		//get the root elememt
		Element docEle = dom.getDocumentElement();
		
		//get a nodelist of <CompositionRoutine> elements
		NodeList nl = docEle.getElementsByTagName("CompositionRoutine");
		
		if(nl != null && nl.getLength() > 0) {
				
			//create one object of Problem for each problem in 
			//Problems array
			//problems= new Problem[nl.getLength()];

			for(int i=0 ; i < nl.getLength() ; i++){
				
				//get the Problem object and store it in 
				//Problems array in index i.
				problems.add(getProblem((Element)nl.item(i)));
			}

		}
	}
	
	private Problem getProblem(Element el) {
		
		//create the new Problem object
		Problem p=new Problem();
		
		//get and set the problem name in Problem object
		//p.setName(el.getAttribute( "name" ));
		p.setName(el.getAttribute( "name" ));
	
		//get a nodelist of <Provided> elements
		NodeList nl = el.getElementsByTagName("Provided");
		
		if(nl != null && nl.getLength() > 0) {
			
			//get the Provided element
			Element el1 = (Element)nl.item(0);
			
			//get the string of inputs of the problem
			String inputStr=el1.getFirstChild().getNodeValue();
			
			String[] inputs = inputStr.split(",");
			
			
			//for each input, add it to the inputs array of 
			//the corresponding Problem object
			for(int i=0; i < inputs.length; i++){
				p.addInput(inputs[i].replace("WSCC:", "").trim());
			}
			
		}
		
		//get a nodelist of <Resultant> elements
		nl = el.getElementsByTagName("Resultant");
		
		if(nl != null && nl.getLength() > 0) {
			
			//get the Resultant element
			Element el1 = (Element)nl.item(0);
			
			//get the string of outputs of the problem
			String outputStr=el1.getFirstChild().getNodeValue();
			
			String[] outputs = outputStr.split(",");
			
			
			//for each output, add it to the outputs array of 
			//the corresponding Problem object
			if(outputs.length > 1){
				for(int i=0; i < outputs.length; i++){
					p.addOutput(outputs[i].replace("WSCC:", "").trim());
				}	
			}else{
				//p.addOutput(outputStr.replace("WSCC:", ""));
				p.addOutput(outputStr.trim());
			}

		}
		return p;
	}
	
	public void printProblems(){
		
		for(Problem p : problems){
			System.out.print("Problem Name: ");
			System.out.println(p.getName() + "\n");

			System.out.println("Inputs:");
			for (int i = 0; i < p.getInputs().size(); i++) {
				System.out.println(p.getInputs().toArray()[i]);
			}

			System.out.println("\nOutputs:");
			for (int i = 0; i < p.getOutputs().size(); i++) {
				System.out.println(p.getOutputs().toArray()[i]);
			}
			System.out.println("\n");
		}
	}
	
//	public void printProblem(int no){
//		//print Problem's name, inputs and outputs by 
//		//problem index in Problems array
//		
//		System.out.print("Problem Name: ");
//		System.out.println(problems[no].getName()+"\n");
//		
//		System.out.println("Inputs:");
//		for(int i = 0 ; i < problems[no].inputs.size(); i++){
//			System.out.println(problems[no].getInputs().toArray()[i]);
//		}
//		
//		System.out.println("\nOutputs:");
//		for(int i = 0 ; i < problems[no].outputs.size(); i++){
//			System.out.println(problems[no].getOutputs().toArray()[i]);
//		}
//	}
}
